Web Request Download

Hello,

Is there a way to make a web request download to a file located at a particular URL?
lsand - Wed Jun 22 11:37:48 EDT 2011

Re: Web Request Download
Mathias Mamsch - Thu Jun 23 07:23:19 EDT 2011

Should be not so hard using msxml2, see for example the VBA example here:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=799

However I guess you can have a couple of problems with this, especially with chr(0) characters. We had another post here, where getting data that contained chr(0) characters failed, so as long as you are referring to text files it should be easy - binary files might be a little harder.

Just post if you need a code example. Regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Web Request Download
lsand - Thu Jun 23 12:23:59 EDT 2011

Mathias Mamsch - Thu Jun 23 07:23:19 EDT 2011
Should be not so hard using msxml2, see for example the VBA example here:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=799

However I guess you can have a couple of problems with this, especially with chr(0) characters. We had another post here, where getting data that contained chr(0) characters failed, so as long as you are referring to text files it should be easy - binary files might be a little harder.

Just post if you need a code example. Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Thanks Mathias,

I would very much like to see an example.

Thank you.

Re: Web Request Download
Mathias Mamsch - Thu Jun 23 12:56:58 EDT 2011

lsand - Thu Jun 23 12:23:59 EDT 2011
Thanks Mathias,

I would very much like to see an example.

Thank you.

Here you go. You need to improve error checking and properly cleanup the ole objects. Hope it helps, regards, Mathias
 

OleAutoArgs args = create
 
void GetFile(string url) {
   OleAutoObj objMsxml = oleCreateAutoObject "MSXML2.XMLHTTP"
   if (null objMsxml) error "Could not create Microsoft XML Request ..."
   
   put(args, "GET")
   put(args, url) 
   put(args, false)
   string err = ""
   err = err oleMethod (objMsxml, "Open", args) 
   if (null err) err = oleMethod (objMsxml, "Send") 
 
   if (!null err) {
        string stat = ""
         print oleGet(objMsxml, "statusText", stat) 
         print "Could not open URL:" err
         print "Status = " stat "\n"
         return 
   } 
   
   int state = 0
   while (state != 4) {
       print oleGet(objMsxml, "readyState", state) 
       sleep_(100)
   }
 
   string s = ""
   print oleGet(objMsxml, "responseText", s) 
   print "FILE = " s 
} 
 
GetFile "http://www.google.com/robots.txt"

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Re: Web Request Download
lsand - Thu Jun 23 16:49:24 EDT 2011

Mathias Mamsch - Thu Jun 23 12:56:58 EDT 2011

Here you go. You need to improve error checking and properly cleanup the ole objects. Hope it helps, regards, Mathias
 

OleAutoArgs args = create
 
void GetFile(string url) {
   OleAutoObj objMsxml = oleCreateAutoObject "MSXML2.XMLHTTP"
   if (null objMsxml) error "Could not create Microsoft XML Request ..."
   
   put(args, "GET")
   put(args, url) 
   put(args, false)
   string err = ""
   err = err oleMethod (objMsxml, "Open", args) 
   if (null err) err = oleMethod (objMsxml, "Send") 
 
   if (!null err) {
        string stat = ""
         print oleGet(objMsxml, "statusText", stat) 
         print "Could not open URL:" err
         print "Status = " stat "\n"
         return 
   } 
   
   int state = 0
   while (state != 4) {
       print oleGet(objMsxml, "readyState", state) 
       sleep_(100)
   }
 
   string s = ""
   print oleGet(objMsxml, "responseText", s) 
   print "FILE = " s 
} 
 
GetFile "http://www.google.com/robots.txt"

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Thank Mathias. How would this work in the case that the document resides at a URL but in order to get to the document you have to log in with a username and password?

thank you.

Re: Web Request Download
Mathias Mamsch - Thu Jun 23 18:43:27 EDT 2011

lsand - Thu Jun 23 16:49:24 EDT 2011
Thank Mathias. How would this work in the case that the document resides at a URL but in order to get to the document you have to log in with a username and password?

thank you.

It should be sufficient to just append username and password to the parameters of the "open" call. See here:

http://msdn.microsoft.com/en-us/library/ms536648%28v=vs.85%29.aspx

Regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS